﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GtsServer.Model;
using GtsServer.Common;
using GtsServer.Manage.Controller;


namespace GtsServer.Manage.IISPage.Ajax
{
    /// <summary>
    /// NoticeAjax 的摘要说明
    /// </summary>
    public class NoticeAjax : IHttpHandler
    {
        NoticeController Controller = new NoticeController();
        public void ProcessRequest(HttpContext context)
        {
            string type = string.Empty;
            if (context.Request["action"] != null)
            {
                type = context.Request["action"];

                switch (type)
                {
                    case "GetList":
                        GetList(context);
                        break;
                    case "Add":
                        Add(context);
                        break;
                    case "Update":
                        Update(context);
                        break;
                    case "Delete":
                        Delete(context);
                        break;
                    case "GetNoticeState":
                        GetNoticeState(context);
                        break;
                    case "GetNoticeType":
                        GetNoticeType(context);
                        break;

                    default:
                        break;
                }
            }
        }

        private void GetNoticeState(HttpContext context)
        {
            List<NoticeStateModel> list = new List<NoticeStateModel>();
            foreach (var item in Enum.GetValues(typeof(EnumNoticeState)))
            {
                NoticeStateModel model = new NoticeStateModel()
                {
                    name = item.ToString(),
                    value = Convert.ToInt32(item)
                };
                list.Add(model);
            }
            context.Response.Write(JsonHelper.ListToJson<NoticeStateModel>(list));
        }
        private void GetNoticeType(HttpContext context)
        {
            List<NoticeByType> list = new List<NoticeByType>();
            foreach (var item in Enum.GetValues(typeof(EnumNoticeType)))
            {
                NoticeByType model = new NoticeByType()
                {
                    Name = item.ToString(),
                    ID = Convert.ToInt32(item)
                };
                list.Add(model);
            }
            context.Response.Write(JsonHelper.ListToJson<NoticeByType>(list));
        }
        private void GetList(HttpContext context)
        {
            string str = "TypeID=" + Convert.ToInt32(EnumNoticeType.全屏公告);
            List<NoticeShow> list = Controller.GetList(str);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].NoticeContent = Microsoft.JScript.GlobalObject.encodeURIComponent(list[i].NoticeContent);
            }
            string json = JsonHelper.ListToJson<NoticeShow>(list);
            //string json = JsonHelper.ToJson(list);
            context.Response.Write(json);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        private void Add(HttpContext context)
        {
            string name = context.Request["name"];
            DateTime CreateTime = Convert.ToDateTime(context.Request["CreateTime"]);
            DateTime startTime = Convert.ToDateTime(context.Request["StartTime"]);
            DateTime endTime = Convert.ToDateTime(context.Request["EndTime"]);
            string NoticeContent = context.Request["NoticeContent"];
            int state = Convert.ToInt32(context.Request["state"]);
            int TypeID = Convert.ToInt32(EnumNoticeType.全屏公告);
            int R = Convert.ToInt32(context.Request["R"]);
            int G = Convert.ToInt32(context.Request["G"]);
            int B = Convert.ToInt32(context.Request["B"]);
            bool IsFullScre = Convert.ToBoolean(context.Request["IsFullScre"]);
            bool IsStick = Convert.ToBoolean(context.Request["IsStick"]);
            bool IsRoll = Convert.ToBoolean(context.Request["IsRoll"]);
            Notice model = new Notice()
            {
                Name = name,
                CreateTime = CreateTime,
                StartTime = startTime,
                EndTime = endTime,
                NoticeContent = NoticeContent,
                TypeID = TypeID,
                State = state,
                IsFullScre = IsFullScre,
                IsStick = IsStick,
                IsRoll = IsRoll
            };
            int id = Controller.Add(model);
            context.Response.Write(id);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        private void Update(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"]);
            string name = context.Request["name"];
            DateTime startTime = Convert.ToDateTime(context.Request["StartTime"]);
            DateTime endTime = Convert.ToDateTime(context.Request["EndTime"]);
            int State = Convert.ToInt32(context.Request["State"]);
            string NoticeContent = context.Request["NoticeContent"];
            Notice model = new Notice()
            {
                ID = id,
                Name = name,
                StartTime = startTime,
                EndTime = endTime,
                State = State,
                NoticeContent = NoticeContent
            };
            context.Response.Write(Controller.Update(model));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private void Delete(HttpContext context)
        {
            int id = Int32.Parse(context.Request["name"]);
            context.Response.Write(Controller.Delete(id));
        }
    }
}